<template>
  <div id="app">
    <div id="up_div">
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
          <el-form-item label="采购申请单号" prop="prCode">
            <el-input
              v-model="queryParams.prCode"
              placeholder="请输入采购申请单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="申请人ID" prop="requesterId">
            <el-input
              v-model="queryParams.requesterId"
              placeholder="请输入申请人ID"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="申请部门ID" prop="departmentId">
            <el-input
              v-model="queryParams.departmentId"
              placeholder="请输入申请部门ID"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="申请提交日期">
            <el-date-picker
              v-model="daterangeRequestDate"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="需求截止日期">
            <el-date-picker
              v-model="daterangeRequiredDate"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="优先级" prop="priority">
            <el-select v-model="queryParams.priority" placeholder="请选择优先级" clearable>
              <el-option
                v-for="dict in dict.type.pms_purchase_requisitions_priority"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in dict.type.pms_purchase_requisitions_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div id="down_div">
      <div class="app-container">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:pmsPurchaseRequisitions:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:pmsPurchaseRequisitions:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:pmsPurchaseRequisitions:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:pmsPurchaseRequisitions:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="pmsPurchaseRequisitionsList" @selection-change="handleSelectionChange" stripe>
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="expand" >
            <template #default="props">
              <h3>商品明细</h3>
              <el-divider></el-divider>
              <el-table :data="props.row.pmsPurchaseRequisitionsDetails" border>
                <el-table-column
                  prop="productName"
                  label="商品名称"
                  width="180">
                </el-table-column>
                <el-table-column label="商品编码" align="center" prop="productCode" />
                <el-table-column label="sku编码" align="center" prop="skuCode" />
                <el-table-column label="规格型号" align="center" prop="productSpec" />
                <el-table-column label="单位" align="center" prop="productUnit" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.wms_product_unit" :value="scope.row.productUnit">
                    </dict-tag>
                  </template>
                </el-table-column>
                <el-table-column label="采购单价" align="center" prop="purchasePrice" />
                <el-table-column label="商品数量" align="center" prop="totalQuantity" />
                <el-table-column label="商品总价" align="center" prop="totalAmount" />
              </el-table>
            </template>
          </el-table-column>
          <el-table-column label="采购申请单号" align="center" prop="prCode" />
          <el-table-column label="申请人" align="center" prop="nickName" />
          <el-table-column label="申请部门" align="center" prop="deptName" />
          <el-table-column label="申请提交日期" align="center" prop="requestDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.requestDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="物料需求截止日期" align="center" prop="requiredDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.requiredDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="优先级" align="center" prop="priority">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pms_purchase_requisitions_priority" :value="scope.row.priority"/>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pms_purchase_requisitions_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="申请总金额(元)" align="center" prop="totalAmount" />
          <el-table-column label="数量" align="center" prop="totalQuantity" />
          <el-table-column label="申请说明" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:pmsPurchaseRequisitions:edit']"
                :disabled = "scope.row.status !== '1'"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-search"
                @click="audit(scope.row)"
                v-hasPermi="['system:pmsPurchaseRequisitions:audit']"
                :disabled = "scope.row.status !== '2'"
              >审核</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:pmsPurchaseRequisitions:remove']"
                :disabled = "scope.row.status !== '1'"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>

  </div>
</template>

<script>
import { listPmsPurchaseRequisitions, getPmsPurchaseRequisitions, delPmsPurchaseRequisitions,
  addPmsPurchaseRequisitions, updatePmsPurchaseRequisitions,getPurchaseRequisitionsData } from "@/api/system/pmsPurchaseRequisitions"

export default {
  name: "PmsPurchaseRequisitions",
  dicts: ['pms_purchase_requisitions_status', 'pms_purchase_requisitions_priority','wms_product_unit'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购申请单主表格数据
      pmsPurchaseRequisitionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 申请说明时间范围
      daterangeRequestDate: [],
      // 申请说明时间范围
      daterangeRequiredDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prCode: null,
        requesterId: null,
        departmentId: null,
        requestDate: null,
        requiredDate: null,
        priority: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        prCode: [
          { required: true, message: "采购申请单号不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询采购申请单主列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeRequestDate && '' != this.daterangeRequestDate) {
        this.queryParams.params["beginRequestDate"] = this.daterangeRequestDate[0]
        this.queryParams.params["endRequestDate"] = this.daterangeRequestDate[1]
      }
      if (null != this.daterangeRequiredDate && '' != this.daterangeRequiredDate) {
        this.queryParams.params["beginRequiredDate"] = this.daterangeRequiredDate[0]
        this.queryParams.params["endRequiredDate"] = this.daterangeRequiredDate[1]
      }
      listPmsPurchaseRequisitions(this.queryParams).then(response => {
        this.pmsPurchaseRequisitionsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        prId: null,
        prCode: null,
        requesterId: null,
        departmentId: null,
        requestDate: null,
        requiredDate: null,
        priority: null,
        status: null,
        totalAmount: null,
        remark: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRequestDate = []
      this.daterangeRequiredDate = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.prId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getPurchaseRequisitionsData().then(response => {
        this.$emit("add",response.data);
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const prId = row.prId || this.ids
      getPmsPurchaseRequisitions(prId).then(response => {
        this.$emit("edit",response.data)
      })
    },
    /** 修改按钮操作 */
    audit(row) {
      this.reset()
      const prId = row.prId || this.ids
      getPmsPurchaseRequisitions(prId).then(response => {
        this.$emit("audit",response.data)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.prId != null) {
            updatePmsPurchaseRequisitions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addPmsPurchaseRequisitions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prIds = row.prId || this.ids
      this.$modal.confirm('是否确认删除采购申请单主编号为"' + prIds + '"的数据项？').then(function() {
        return delPmsPurchaseRequisitions(prIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pmsPurchaseRequisitions/export', {
        ...this.queryParams
      }, `pmsPurchaseRequisitions_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
body, html {
  margin: 0;
  padding: 0;
  height: 100%;
}

#app {
  //background-color: #f4f4f4;
  background-color: #ffffff;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 3px;
}

#up_div {
  background-color: white;
  height: 30%;
  width: 100%;
  margin-bottom: 20px; /* Space between the two divs */
  box-sizing: border-box;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  border-radius: 4px; /* Optional rounded corners */
}

#down_div {
  background-color: white;
  height: 70%;
  width: 100%;
  box-sizing: border-box;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  border-radius: 4px; /* Optional rounded corners */
}

</style>
